
#include "StateShopCommon.h"
#include "StateCoinsPopUp.h"
#include "StatePopUp.h"

#include "StateMainMenu.h"
#include "StateManager.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "ProgressManager.h"
#include "LevelManager.h"

void StateShopCommon::loadContent()
{
    m_oBg = new RenderableEntity("MENU_0010_bg", Vector2(320, 480));
    m_oBg->setCentered();
    
    m_oTitleBg = new RenderableEntity("MENU_0009_ribbon", Vector2(320, 870));
    m_oTitleBg->setCentered();
    m_oTitleBg->setLiving(true);
    
    m_oTitleText = new TextEntity("Babies", FONT_NAME, 60, Vector2(320, 885), 1, 1);
    m_oTitleText->setCentered();
    m_oTitleText->setShadow(true, COLOR_PINK_DARK);
    m_oTitleText->setColor(COLOR_PINK_TEXT);


    m_oCoinsText = new TextEntity(" ", FONT_NAME, 40, Vector2(570, 160), 2, 1);
    m_oCoinsText->setCenter(Vector2(1.0f, 0.5f));
    m_oCoinsText->setShadow();
    
    m_oCoinsIcon = new RenderableEntity("icon_babycoin", Vector2(570, 160));
    m_oCoinsIcon->setCenter(Vector2(0.0f, 0.5f));
    m_oCoinsIcon->setScale(Vector2(0.5f, 0.5f));
    
    
    m_oBuyCoinsIcon = new RenderableEntity("buicoins", Vector2(525, 85));
    m_oBuyCoinsIcon->setCentered();
    m_oBuyCoinsIcon->changeColorWhenClicked(true);
    m_oBuyCoinsIcon->setupButton(Vector2(0.8f, 0.8f));
    
    m_oBuyCoinsText = new TextEntity("Buy Coins", FONT_NAME, 28, Vector2(522, 40), 1, 1);
    m_oBuyCoinsText->setCentered();
    m_oBuyCoinsText->setShadow();
    m_oBuyCoinsText->setColor(COLOR_PINK_TEXT);
    
    m_oBack = new RenderableEntity("MENU_0008_back", Vector2(80, 80));
    m_oBack->changeColorWhenClicked(true);
    m_oBack->setCentered();
    m_oBack->setLiving(true);
    
    
    m_oEnter = new TimeLineEffect();
    m_oEnter->addInEffect(m_oTitleBg);
    m_oEnter->addInEffect(m_oTitleText);
    m_oEnter->addInEffect(m_oBack);
    
    m_oExit = new TimeLineEffect();
    m_oExit->addOutEffect(m_oTitleBg);
    m_oExit->addOutEffect(m_oTitleText);
    m_oExit->addOutEffect(m_oBack);
    
    m_ePopupState = SHOPPOPUP_NONE;
}

void StateShopCommon::unloadContent()
{
    delete m_oBg;
}

void StateShopCommon::activated()
{
    enter();
}

void StateShopCommon::enter()
{
    switch(m_ePopupState)
    {
        case SHOPPOPUP_NONE:
            m_eState = SHOP_ENTER;
            m_oEnter->start(true);
            break;
            
        case SHOPPOPUP_NOCOINS:
            if(StatePopUp::instance().getResult())
            {
                //Pop up coins
                StateCoinsPopUp::instance().popup();
            }
            break;
            
        default:
            break;
    }
    
    if(m_ePopupState == SHOPPOPUP_NOCOINS)
        m_ePopupState = SHOPPOPUP_BUYCOINS;
    else
        m_ePopupState = SHOPPOPUP_NONE;
    
    char buff[10];
    sprintf(buff, "%i", ProgressManager::instance().getSavedData().iCoins);
    m_oCoinsText->setText(buff);
}

void StateShopCommon::updatePaused(float dt)
{
    switch(m_eState)
    {
        case SHOP_IDLE:
            break;
            
        default:
            break;
    }
}

void StateShopCommon::update(float dt)
{
    switch(m_eState)
    {
        case SHOP_ENTER:
            if(m_oEnter->isFinished())
            {
                m_eState = SHOP_IDLE;
            }
            break;
            
        case SHOP_IDLE:
            m_oTitleBg->update();
            m_oTitleText->setScale(m_oTitleBg->getScale());

            m_oBack->update();    
            m_oBuyCoinsIcon->update();
                        
            if(m_oBack->isClicked())
            {
                setBackState();
                m_oExit->start(true);
                m_eState = SHOP_EXIT;
            }
            else if(m_oBuyCoinsIcon->isClicked())
            {
                m_ePopupState = SHOPPOPUP_BUYCOINS;
                StateCoinsPopUp::instance().popup();
            }
            
            break;
            
        case SHOP_EXIT:
            if(m_oExit->isFinished())
            {
                StateManager::instance().setState(*m_oNextState);
            }
            
            break;
    }
}

void StateShopCommon::render()
{
    m_oBg->render();
    
    m_oTitleBg->render();
    m_oTitleText->render();
    
    
    m_oBuyCoinsIcon->render();
    m_oBuyCoinsText->render();
    
    m_oCoinsIcon->render();
    m_oCoinsText->render();
    
    m_oBack->render();
}

void StateShopCommon::exit()
{
}

std::string StateShopCommon::getPrizeStr(int _iPrize)
{
    char buff[10];
    if(_iPrize >= 10000)
    {
        sprintf(buff, "%ik", _iPrize / 1000);
    }
    else 
    {
        sprintf(buff, "%i", _iPrize);
    }
    return buff;
}
